package org.bot.plugin.data.event;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.bot.api.event.DataStorage;
import org.bot.api.event.EventName;
import org.bot.api.event.Events;
import org.bot.api.flow.FlowNode;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 沧月
 * @Date: 2024/1/11 17:30
 * @Description: 数据合并到对象
 */
@Component
@EventName("合并数据")
public class DataMergeToObject implements Events {

    @Override
    public Object exec(FlowNode node, DataStorage data) {
        String arg = node.getArg();
        if (arg == null) {
            return new JSONObject();
        }

        JSONArray array = JSONArray.parseArray(arg);
        JSONObject _data = new JSONObject();
        array.forEach(o -> {
            JSONObject _tmp = (JSONObject) o;
            _data.fluentPut(_tmp.getString("key"), data.get(_tmp.getString("arg")));
        });
        return _data;
    }
}
